package com.czxy.controller;

import com.czxy.domain.Customer;
import com.czxy.domain.LinkMan;
import com.czxy.service.LinkManService;
import com.github.pagehelper.PageInfo;
import org.omg.CORBA.INTERNAL;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/linkMan")
public class LinkManController {

    @Resource
    private LinkManService linkManService;

    @RequestMapping("/findByInfo")
    public String findByInfo(Integer pageNum, Integer pageSize, LinkMan linkMan, Model model){

        //调用Service 获取pageInfo
        PageInfo byInfo = linkManService.findByInfo(pageNum, pageSize,linkMan);

        // 存储到request域
        model.addAttribute("pageInfo", byInfo);
        model.addAttribute("linkMan", linkMan);
        // 请求转发
        return "linkman/list";
    }



    @RequestMapping("/addUI")
    public String addUI(Model model){
        List<Customer> allCustomers = linkManService.findAllCustomers();
        model.addAttribute("allCustomers",allCustomers);
        return "linkman/add";
    }


    @RequestMapping("/add")
    public String add(LinkMan linkMan){
        linkManService.add(linkMan);
        return "redirect:/linkMan/findByInfo.action?pageNum=1&pageSize=3";
    }



    @RequestMapping("/delete")
    public String delete(LinkMan linkMan){
        linkManService.del(linkMan);
        return "redirect:/linkMan/findByInfo.action?pageNum=1&pageSize=3";
    }


    @RequestMapping("/editUI")
    public String editUI(LinkMan linkMan, Model model){

        List<Customer> allCustomers = linkManService.findAllCustomers();
        LinkMan linkManById = linkManService.findById(linkMan);

        model.addAttribute("allCustomers",allCustomers);
        model.addAttribute("linkManById",linkManById);

        return "linkman/edit";
    }

    @RequestMapping("/edit")
    public String edit(LinkMan linkMan){
        linkManService.update(linkMan);
        return "redirect:/linkMan/findByInfo.action?pageNum=1&pageSize=3";
    }

}
